﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using Jungle.Code.Core;
using System.Text;
using System.Security.Cryptography;

using Jungle.Users;

	public partial class RePassUser : BaseUserModule
	{
        protected override void OnInit(EventArgs e)
        {
            btn_repass.PostBackUrl = Request.RawUrl;
            base.OnInit(e);
        }

        protected void RePass(object sender, EventArgs e)
        {
            bool HaveEmail = false;
            string email = "";
            email = tb_email.Text.Trim();
            UserManager um = new UserManager();
            um.GetAll();
            foreach(UserManager u in um.UserList)
                if (u.Email == email)
                {
                    HaveEmail = true;
                    string pass = GetRandom("");
                    string body = "В этом письме находится доступ к аккаунту в проекте Alex Consult \r\n Email: " + u.Email + "\r\n Пароль: " + pass;
                    UserManager user = u;
                    u.Password = UserManager.getMd5Hash(pass);
                    u.Update();
                    UserManager.SendEmail(email, "Jungle.net.ua: Востановление пароля", body);
                }
            if (HaveEmail)
                ValidBlock.InnerHtml = "We send new password on your email";
            else
                ValidBlock.InnerHtml = "<strong style=\"color: #F00;\">Incorrect email</strong>";
        }

        protected string GetRandom(string str)
        {
            string mask = "1234567890qwertyuiopasdfghjklzxcvbnm";
            System.Random rnd = new Random();
            for (int i = 0; i < 6; i++)
            {
                str += mask[rnd.Next(0, 35)];
            }
            return str;
        }
	}
